﻿using Microsoft.Xna.Framework.Graphics;
using PointShop.ShopSystem;
using ReLogic.Content;
using System;
using Terraria;
using Terraria.ModLoader;
using 积分商店扩展.Common.ModUtils;

namespace 积分商店扩展.Common.Systems
{
    public class 解锁条件 : ModSystem
    {
        public override void Load()
        {
            积分商店扩展 instance;
            instance = ModContent.GetInstance<积分商店扩展>();
            Func<bool> downedHiveMindCondition;
            Func<bool> downedPerforatorCondition;
            if (instance != null)
            {
                PointShopSystem.RegisterUnlockCondition((Mod)(object)instance, "DownedSkeletron", ModContent.Request<Texture2D>("积分商店扩展/Assets/Textures/UnlockIcon/Skeletron", (AssetRequestMode)2), (Func<bool>)Condition.DownedSkeletron.IsMet);
                PointShopSystem.RegisterUnlockCondition((Mod)(object)instance, "DownedMoonLord", ModContent.Request<Texture2D>("积分商店扩展/Assets/Textures/UnlockIcon/MoonLord", (AssetRequestMode)2), (Func<bool>)Condition.DownedMoonLord.IsMet);
                PointShopSystem.RegisterUnlockCondition((Mod)(object)instance, "DayTime", ModContent.Request<Texture2D>("积分商店扩展/Assets/Textures/UnlockIcon/DayTime", (AssetRequestMode)2), (Func<bool>)Condition.TimeDay.IsMet);
                if (ModLoader.TryGetMod("CalamityMod", out Mod mod))
                {
                    downedHiveMindCondition = MyUtils.GetDownedBossCondition(mod, "CalamityMod.DownedBossSystem", "_downedHiveMind");
                    downedPerforatorCondition = MyUtils.GetDownedBossCondition(mod, "CalamityMod.DownedBossSystem", "_downedPerforator");
                    PointShopSystem.RegisterUnlockCondition((Mod)(object)instance, "EitherCondition", ModContent.Request<Texture2D>(isCorruption() ? "积分商店扩展/Assets/Textures/UnlockIcon/HiveMind" : "积分商店扩展/Assets/Textures/UnlockIcon/PerforatorHive", (AssetRequestMode)2), eitherCondition);
                    Func<bool> downedBossCondition;
                    downedBossCondition = MyUtils.GetDownedBossCondition(mod, "CalamityMod.DownedBossSystem", "_downedAstrumDeus");
                    PointShopSystem.RegisterUnlockCondition((Mod)(object)instance, "DownedAstrumDeus", ModContent.Request<Texture2D>("积分商店扩展/Assets/Textures/UnlockIcon/AstrumDeus", (AssetRequestMode)2), downedBossCondition);
                    Func<bool> downedBossCondition2;
                    downedBossCondition2 = MyUtils.GetDownedBossCondition(mod, "CalamityMod.DownedBossSystem", "_downedAstrumAureus");
                    PointShopSystem.RegisterUnlockCondition((Mod)(object)instance, "DownedAstrumAureus", ModContent.Request<Texture2D>("积分商店扩展/Assets/Textures/UnlockIcon/AstrumAureus", (AssetRequestMode)2), downedBossCondition2);
                    Func<bool> downedBossCondition3;
                    downedBossCondition3 = MyUtils.GetDownedBossCondition(mod, "CalamityMod.DownedBossSystem", "_downedProvidence");
                    PointShopSystem.RegisterUnlockCondition((Mod)(object)instance, "DownedProvidence", ModContent.Request<Texture2D>("积分商店扩展/Assets/Textures/UnlockIcon/Providence", (AssetRequestMode)2), downedBossCondition3);
                    Func<bool> downedBossCondition4;
                    downedBossCondition4 = MyUtils.GetDownedBossCondition(mod, "CalamityMod.DownedBossSystem", "_downedDesertScourge");
                    PointShopSystem.RegisterUnlockCondition((Mod)(object)instance, "DownedDesertScourge", ModContent.Request<Texture2D>("积分商店扩展/Assets/Textures/UnlockIcon/DesertScourge", (AssetRequestMode)2), downedBossCondition4);
                    Func<bool> downedBossCondition5;
                    downedBossCondition5 = MyUtils.GetDownedBossCondition(mod, "CalamityMod.DownedBossSystem", "_downedPolterghast");
                    PointShopSystem.RegisterUnlockCondition((Mod)(object)instance, "DownedPolterghast", ModContent.Request<Texture2D>("积分商店扩展/Assets/Textures/UnlockIcon/Polterghast", (AssetRequestMode)2), downedBossCondition5);
                }
            }
            bool eitherCondition()
            {
                if (!isCorruption() || !downedHiveMindCondition())
                {
                    if (isCrimson())
                    {
                        return downedPerforatorCondition();
                    }
                    return false;
                }
                return true;
            }
            static bool isCorruption()
            {
                return !WorldGen.crimson;
            }
            static bool isCrimson()
            {
                return WorldGen.crimson;
            }
        }
    }
}